from django.db import models

from oplatform.constant import DELIVERY_SYSTEM, CODERATE, TUNER, MODULATION, SWITCH, PSI_TABLE_FILTER, STATUS
from utils.time import get_current_time_by_microsecond


class ConfigurationData(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(default='', max_length=100, null=False)
    delivery_system = models.IntegerField(default=4, choices=DELIVERY_SYSTEM)
    freq = models.IntegerField(null=True)
    srate = models.IntegerField(null=True)
    coderate = models.IntegerField(choices=CODERATE, default=9, null=True)
    tuner = models.IntegerField(default=0, choices=TUNER, null=True)
    modulation = models.IntegerField(default=6, choices=MODULATION, null=True)
    multicast_iface4 = models.CharField(max_length=100, null=True)
    multicast_ipv4 = models.IntegerField(default=1, choices=SWITCH, null=True)
    common_port = models.IntegerField(default=1234, null=True)
    multicast_ttl = models.IntegerField(default=2, null=True)
    autoconfiguration = models.CharField(default='full', max_length=100, null=True)
    multicast_auto_join = models.IntegerField(default=0, null=True)
    ip_http = models.GenericIPAddressField(protocol='IPv4', default='127.0.0.1', null=True)
    dvr_buffer_size = models.IntegerField(default=20, null=True)
    dvr_thread = models.IntegerField(default=0, choices=SWITCH, null=True)
    dvr_thread_buffer_size = models.IntegerField(default=5000, null=True)
    scam_support = models.IntegerField(default=0, choices=SWITCH, null=True)
    ring_buffer_default_size = models.IntegerField(default=32768, null=True)
    decsa_default_delay = models.IntegerField(null=True)
    send_default_delay = models.IntegerField(null=True)
    unicast = models.IntegerField(default=1, choices=SWITCH, null=True)
    port_http = models.CharField(null=True, max_length=100, default='8000+%card.%number')
    psi_tables_filtering = models.IntegerField(default=0, choices=PSI_TABLE_FILTER, null=True)
    autoconf_sid_list = models.CharField(default='', max_length=100, null=True)
    status = models.IntegerField(default=0, choices=STATUS, null=True)
    channels = models.ManyToManyField('ConfigurationChannel')

    class Meta:
        db_table = 'configuration'


class ConfigurationChannel(models.Model):
    SWITCH = (
        (0, '关闭'),
        (1, '开启'),
    )
    id = models.AutoField(primary_key=True)
    name = models.CharField(null=False, max_length=1000)
    ip = models.GenericIPAddressField(protocol='IPv4', null=False, default='224.2.%card.%number')
    service_id = models.IntegerField(null=False)
    oscam = models.IntegerField(default=1, null=False, choices=SWITCH)
    port = models.IntegerField(default=1234, null=False)

    class Meta:
        db_table = 'configurationchannel'


class ChannelData(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(null=False, max_length=100, default='')
    provider_name = models.CharField(null=False, max_length=100, default='')
    coderate = models.CharField(null=False, default='', max_length=100)
    freq = models.IntegerField(null=False, default=0)
    modulation = models.CharField(null=False, max_length=100, default='')
    delivery_system = models.CharField(default='', null=False, max_length=100)
    srate = models.IntegerField(null=False, default=0)
    sid = models.IntegerField(null=False, default=0)
    pmt = models.IntegerField(null=False, default=0)
    pcr = models.IntegerField(null=False, default=0)
    video_id = models.CharField(null=False, max_length=100, default='')
    audio_id = models.CharField(null=False, max_length=100, default='')
    subtitle = models.IntegerField(null=False, default=0)
    caid = models.CharField(null=False, default='', max_length=100)

    class Meta:
        db_table = 'channeldata'


class Menu(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=255, default='')
    expandable = models.BooleanField(default=0)
    bold = models.BooleanField(default=0)
    css_icon = models.CharField(max_length=255, default='')
    link = models.CharField(max_length=255, default='')
    father = models.ForeignKey("self", on_delete=models.CASCADE, null=True, related_name='father_set',
                               )
    supers = models.ManyToManyField("self", symmetrical=False)

    class Meta:
        db_table = 'menu'


class User(models.Model):
    id = models.AutoField(primary_key=True)
    username = models.CharField(max_length=50, default='')
    password = models.CharField(max_length=255, default='')
    alias = models.CharField(max_length=128, default='')

    _is_authenticated = False

    @property
    def is_authenticated(self):
        return self._is_authenticated

    def authenticate(self):
        self._is_authenticated = True

    class Meta:
        db_table = "user"


class MachineCodeData(models.Model):
    id = models.AutoField(primary_key=True)
    code = models.TextField(default='', null=False)
    start_time = models.BigIntegerField(null=True, default=get_current_time_by_microsecond())
    end_time = models.BigIntegerField(null=True, default=get_current_time_by_microsecond())

    class Meta:
        db_table = 'machine_code'

